/*
 * Checkstyle4nb is a plugin for netbeans for checking Java coding conventions.
 * Copyright (C) 2006-2007, Andrea Matsunaga
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.ammatsun.checkstyle4nb.actions;

import java.io.File;
import javax.swing.JFileChooser;

/**
 * Panel that generates the GUI where the user can set the checkstyle module
 * settings.
 *
 * @version    1.0, 2006/Jul/12
 * @author     Andrea Matsunaga
 */
public class CheckstyleOptionsPanel extends javax.swing.JPanel {

    /** Creates new form CheckstyleOptionsPanel. */
    public CheckstyleOptionsPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        jLabel1.setDisplayedMnemonic('C');
        jLabel1.setLabelFor(jTextField1);
        jLabel1.setText("Checkstyle Configuration File:");
        jLabel1.setToolTipText("Choose a checkstyle configuration file in XML.");

        jButton1.setMnemonic('B');
        jButton1.setText("Browse...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setDisplayedMnemonic('P');
        jLabel2.setLabelFor(jTextField2);
        jLabel2.setText("Checkstyle Properties File:");
        jLabel2.setToolTipText("Choose a properties file with extra configuration for checkstyle.");

        jButton2.setMnemonic('r');
        jButton2.setText("Browse...");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jTextField2)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton1)
                    .add(jButton2))
                .addContainerGap(511, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton2))
                .addContainerGap(369, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * When the first browse button is clicked, the user gets to select
     * a file, which is then filled into the corresponding text field
     * for the checkstyle properties file.
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JFileChooser chooser = new JFileChooser(new File(jTextField2.getText()));
        // Show open dialog; method does not return until the dialog is closed
        int result = chooser.showOpenDialog(this);
        // Determine which button was clicked to close the dialog
        switch (result) {
            case JFileChooser.APPROVE_OPTION:
                File selFile = chooser.getSelectedFile();
                jTextField2.setText(selFile.getAbsolutePath());
                break;
            case JFileChooser.CANCEL_OPTION:
                // Cancel or the close-dialog icon was clicked
                break;
            case JFileChooser.ERROR_OPTION:
                // The selection process did not complete successfully
                break;
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * When the first browse button is clicked, the user gets to select
     * a file, which is then filled into the corresponding text field
     * for the checkstyle configuration file.
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser chooser = new JFileChooser(new File(jTextField1.getText()));
        // Show open dialog; this method does not return until the dialog is closed
        int result = chooser.showOpenDialog(this);
        // Determine which button was clicked to close the dialog
        switch (result) {
            case JFileChooser.APPROVE_OPTION:
                File selFile = chooser.getSelectedFile();
                jTextField1.setText(selFile.getAbsolutePath());
                break;
            case JFileChooser.CANCEL_OPTION:
                // Cancel or the close-dialog icon was clicked
                break;
            case JFileChooser.ERROR_OPTION:
                // The selection process did not complete successfully
                break;
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * Retrieve the value in text field as checkstyle configuration filename.
     * @return a String with the checkstyle configuration filename
     */
    public String getConfigurationFile() {
        return jTextField1.getText().trim();
    }

    /**
     * Sets the value in text field with checkstyle configuration filename.
     * @param filename new value for checkstyle configuration filename
     */
    public void setConfigurationFile(String filename) {
        if (null != filename) {
            jTextField1.setText(filename.trim());
        }
    }

    /**
     * Retrieve the value in text field as checkstyle properties filename.
     * @return a String with the checkstyle properties filename
     */
    public String getPropertiesFile() {
        return jTextField2.getText().trim();
    }

    /**
     * Sets the value in text field with checkstyle properties filename.
     * @param filename new value for checkstyle properties filename
     */
    public void setPropertiesFile(String filename) {
        if (null != filename) {
            jTextField2.setText(filename.trim());
        }
    }

    /**
     * Indicates if the values set in the panel are valid.
     * @return true if the values set for configuration and properties file
     * are valid, false otherwise.
     */
    public boolean valid() {
        return ((null != getConfigurationFile())
                && ((new File(getConfigurationFile())).exists())
                && (null != getPropertiesFile())
                && ((0 == getPropertiesFile().length())
                || ((new File(getPropertiesFile())).exists())));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
